package com.alibaba.citrus.ots.application.report.ability.repository.impl;

import com.alibaba.citrus.ots.application.report.ability.repository.MonthSalesBasicReportQueryRepository;
import com.alibaba.cz.base.tool.Nullable;
import com.alibaba.cz.base.tool.StringUtil;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsapplicationreportsdk.contants.OtsApplicationReportSdkConstants;
import com.epoch.app.otsapplicationreportsdk.model.dto.CommPageQueryRequest;
import com.epoch.app.otsapplicationreportsdk.model.dto.MonthSalesBasicReportLoadListRequest;
import com.epoch.app.otsapplicationreportsdk.model.dto.MonthSalesBasicReportLoadListVO;
import com.epoch.app.otsapplicationreportsdk.model.dto.MonthSalesBasicReportSDO;
import com.epoch.app.otsapplicationreportsdk.model.dto.QueryMonthSalesReportRequest;
import com.epoch.app.otsapplicationreportsdk.monthsalesbasicreport.dto.PageStatisticalMonthSalesCategoryReadonlyRequest;
import com.epoch.app.otsapplicationreportsdk.monthsalesbasicreport.dto.PageStatisticalMonthSalesCategoryRequest;
import com.epoch.app.otsapplicationreportsdk.monthsalesbasicreport.dto.PageStatisticalMonthSalesChannelRankRequest;
import com.epoch.app.otsapplicationreportsdk.monthsalesbasicreport.dto.PageStatisticalMonthSalesDepartmentRankRequest;
import com.epoch.app.otsapplicationreportsdk.monthsalesbasicreport.dto.PageStatisticalMonthSalesEmployeeRankRequest;
import com.epoch.app.otsapplicationreportsdk.monthsalesbasicreport.dto.StatisticalRepeatKeyRequest;
import com.epoch.app.otsapplicationreportsdk.monthsalesbasicreport.model.MonthSalesBasicReport;
import com.epoch.app.otsapplicationreportsdk.monthsalesbasicreport.service.MonthSalesBasicReportService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhoubc
 * @description: TODO
 * @date 2021/11/11 23:07
 */
@Component
public class MonthSalesBasicReportQueryRepositoryImpl implements MonthSalesBasicReportQueryRepository {

    @Resource
    MonthSalesBasicReportService monthSalesBasicReportService;

    @Override
    public PageResult<List<MonthSalesBasicReportSDO>> pageQueryList(QueryMonthSalesReportRequest request) {
        MonthSalesBasicReportLoadListVO monthSalesBasicReportLoadListVO = loadListRequestConverter(request);
        Result<List<MonthSalesBasicReport>> response = null;
        if("1".equals(OtsApplicationReportSdkConstants.QUERY_READONLY_DATABASE())){
            response = monthSalesBasicReportService.loadListForPageReadonly(monthSalesBasicReportLoadListVO);
        }else{
            response = monthSalesBasicReportService.loadListForPage(monthSalesBasicReportLoadListVO);
        }
        List<MonthSalesBasicReportSDO> result = response.getResult().stream().map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            bean.setId(String.valueOf(x.getId()));
            return bean;
        }).collect(Collectors.toList());
        return PageResult.of(result,response.getTotal());
    }

    @Override
    public PageResult<List<MonthSalesBasicReportSDO>> pageQueryMonthSalesBasicReportList(QueryMonthSalesReportRequest request) {
        MonthSalesBasicReportLoadListVO monthSalesBasicReportLoadListVO = loadListRequestConverter(request);
        Result<List<MonthSalesBasicReport>> response = monthSalesBasicReportService.loadListForPageSort(monthSalesBasicReportLoadListVO);
        List<MonthSalesBasicReportSDO> result = response.getResult().stream().map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            bean.setId(String.valueOf(x.getId()));
            return bean;
        }).collect(Collectors.toList());
        return PageResult.of(result,response.getTotal());
    }

    @Override
    public List<MonthSalesBasicReportSDO> statisticalQuery(QueryMonthSalesReportRequest request) {
        MonthSalesBasicReportLoadListVO monthSalesBasicReportLoadListVO = loadListRequestConverter(request);
        Result<List<MonthSalesBasicReport>> response = monthSalesBasicReportService.statisticalQuery(monthSalesBasicReportLoadListVO);
        List<MonthSalesBasicReportSDO> result = response.getResult().stream().map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            bean.setId(String.valueOf(x.getId()));
            return bean;
        }).collect(Collectors.toList());
        return result;
    }

    @Override
    public List<MonthSalesBasicReportSDO> statisticalSalesRankQuery(QueryMonthSalesReportRequest request) {
        MonthSalesBasicReportLoadListVO monthSalesBasicReportLoadListVO = loadListRequestConverter(request);
        Result<List<MonthSalesBasicReport>> response = monthSalesBasicReportService.statisticalSalesRankQuery(monthSalesBasicReportLoadListVO);
        List<MonthSalesBasicReportSDO> result = response.getResult().stream().map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            bean.setId(String.valueOf(x.getId()));
            return bean;
        }).collect(Collectors.toList());
        return result;
    }

    @Override
    public List<MonthSalesBasicReportSDO> statisticalScItemMarkQuery(QueryMonthSalesReportRequest request) {
        MonthSalesBasicReportLoadListVO monthSalesBasicReportLoadListVO = loadListRequestConverter(request);
        Result<List<MonthSalesBasicReport>> response = monthSalesBasicReportService.statisticalScItemMarkQuery(monthSalesBasicReportLoadListVO);
        List<MonthSalesBasicReportSDO> result = response.getResult().stream().map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            bean.setId(String.valueOf(x.getId()));
            return bean;
        }).collect(Collectors.toList());
        return result;
    }

    @Override
    public List<MonthSalesBasicReportSDO> getMonthSalesBasicReportList(QueryMonthSalesReportRequest request) {
        MonthSalesBasicReportLoadListRequest monthSalesBasicReportRequest = getRequestConverter(request);
        Result<List<MonthSalesBasicReport>> response = monthSalesBasicReportService.loadList(monthSalesBasicReportRequest);
        List<MonthSalesBasicReportSDO> result = response.getResult().stream().map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            bean.setId(String.valueOf(x.getId()));
            return bean;
        }).collect(Collectors.toList());
        return result;
    }

    @Override
    public PageResult<List<MonthSalesBasicReportSDO>> pageStatisticalMonthSalesEmployeeRank(CommPageQueryRequest request) {
        PageStatisticalMonthSalesEmployeeRankRequest queryRequest = new PageStatisticalMonthSalesEmployeeRankRequest();
        queryRequest.setStatisticalDate(request.getStatDate());
        queryRequest.setPlanMonth(request.getPlanMonth());
        queryRequest.setDir(request.getDir());
        queryRequest.setStart(request.getStart());
        queryRequest.setLimit(request.getLimit());
        queryRequest.setOrgSalesDepartmentId(request.getOrgSalesDepartmentCode());
        Result<List<MonthSalesBasicReport>> response = monthSalesBasicReportService.pageStatisticalMonthSalesEmployeeRank(queryRequest);
        List<MonthSalesBasicReportSDO> result = response.getResult().stream().map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            bean.setId(String.valueOf(x.getId()));
            return bean;
        }).collect(Collectors.toList());
        return PageResult.of(result,response.getTotal());
    }

    @Override
    public PageResult<List<MonthSalesBasicReportSDO>> pageStatisticalMonthSalesDepartmentRank(CommPageQueryRequest request) {
        PageStatisticalMonthSalesDepartmentRankRequest queryRequest = new PageStatisticalMonthSalesDepartmentRankRequest();
        queryRequest.setStatisticalDate(request.getStatDate());
        queryRequest.setPlanMonth(request.getPlanMonth());
        queryRequest.setDir(request.getDir());
        queryRequest.setStart(request.getStart());
        queryRequest.setLimit(request.getLimit());
        queryRequest.setOrgSalesDepartmentId(request.getOrgSalesDepartmentCode());
        Result<List<MonthSalesBasicReport>> response = monthSalesBasicReportService.pageStatisticalMonthSalesDepartmentRank(queryRequest);
        List<MonthSalesBasicReportSDO> result = response.getResult().stream().map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            bean.setId(String.valueOf(x.getId()));
            return bean;
        }).collect(Collectors.toList());
        return PageResult.of(result,response.getTotal());
    }

    @Override
    public PageResult<List<MonthSalesBasicReportSDO>> pageStatisticalMonthSalesChannelRank(CommPageQueryRequest request) {
        PageStatisticalMonthSalesChannelRankRequest queryRequest = new PageStatisticalMonthSalesChannelRankRequest();
        queryRequest.setStatisticalDate(request.getStatDate());
        queryRequest.setPlanMonth(request.getPlanMonth());
        queryRequest.setDir(request.getDir());
        queryRequest.setStart(request.getStart());
        queryRequest.setLimit(request.getLimit());
        Result<List<MonthSalesBasicReport>> response = monthSalesBasicReportService.pageStatisticalMonthSalesChannelRank(queryRequest);
        List<MonthSalesBasicReportSDO> result = response.getResult().stream().map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            bean.setId(String.valueOf(x.getId()));
            return bean;
        }).collect(Collectors.toList());
        return PageResult.of(result,response.getTotal());
    }

    @Override
    public PageResult<List<MonthSalesBasicReportSDO>> pageStatisticalMonthSalesCategory(CommPageQueryRequest request) {
        Result<List<MonthSalesBasicReport>> response = null;
        if("1".equals(OtsApplicationReportSdkConstants.QUERY_READONLY_DATABASE())){
            PageStatisticalMonthSalesCategoryReadonlyRequest queryRequest = new PageStatisticalMonthSalesCategoryReadonlyRequest();
            queryRequest.setStatisticalDate(request.getStatDate());
            queryRequest.setPlanMonth(request.getPlanMonth());
            queryRequest.setBizType(request.getBizType());
            queryRequest.setStart(request.getStart());
            queryRequest.setLimit(request.getLimit());
            queryRequest.setOrgSalesDepartmentId(request.getOrgSalesDepartmentCode());
            response = monthSalesBasicReportService.pageStatisticalMonthSalesCategoryReadonly(queryRequest);
        }else{
            PageStatisticalMonthSalesCategoryRequest queryRequest = new PageStatisticalMonthSalesCategoryRequest();
            queryRequest.setStatisticalDate(request.getStatDate());
            queryRequest.setPlanMonth(request.getPlanMonth());
            queryRequest.setBizType(request.getBizType());
            queryRequest.setStart(request.getStart());
            queryRequest.setLimit(request.getLimit());
            queryRequest.setOrgSalesDepartmentId(request.getOrgSalesDepartmentCode());
            response = monthSalesBasicReportService.pageStatisticalMonthSalesCategory(queryRequest);
        }
        List<MonthSalesBasicReportSDO> result = response.getResult().stream().map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            if(StringUtil.isNotEmpty(x.getScItemRootCategoryId())){
                bean.setScItemCategoryId(x.getScItemRootCategoryId());
                bean.setScItemCategoryName(x.getScItemRootCategoryName());
            }
            if(StringUtil.isNotEmpty(x.getScItemLeafCategoryId())){
                bean.setScItemCategoryId(x.getScItemLeafCategoryId());
                bean.setScItemCategoryName(x.getScItemLeafCategoryName());
            }
            bean.setId(String.valueOf(x.getId()));
            return bean;
        }).collect(Collectors.toList());
        return PageResult.of(result,response.getTotal());
    }

    @Override
    public List<Long> statisticalRepeatKey(String statisticalDate) {
        StatisticalRepeatKeyRequest statisticalRepeatKeyRequest = new StatisticalRepeatKeyRequest();
        statisticalRepeatKeyRequest.setStatisticalDate(statisticalDate);
        statisticalRepeatKeyRequest.setStart(0);
        statisticalRepeatKeyRequest.setLimit(1000);
        Result<List<MonthSalesBasicReport>> result = monthSalesBasicReportService.statisticalRepeatKey(statisticalRepeatKeyRequest);
        List<Long> ids = Nullable.stream(result.getResult()).map(MonthSalesBasicReport::getId).collect(Collectors.toList());
        return ids;
    }

//    @Override
//    public PageResult<List<MonthSalesBasicReportSDO>> pageStatisticalMonthSalesCompanyCategory(CommPageQueryRequest request) {
//        PageStatisticalMonthSalesCompanyCategoryRequest queryRequest = new PageStatisticalMonthSalesCompanyCategoryRequest();
//        queryRequest.setStatisticalDate(request.getStatDate());
//        queryRequest.setPlanMonth(request.getPlanMonth());
//        queryRequest.setBizType(request.getBizType());
//        queryRequest.setStart(request.getStart());
//        queryRequest.setLimit(request.getLimit());
//        queryRequest.setOrgBusinessUnitId(request.getOrgBusinessUnitCode());
//        Result<List<MonthSalesBasicReport>> response = monthSalesBasicReportService.pageStatisticalMonthSalesCompanyCategory(queryRequest);
//        List<MonthSalesBasicReportSDO> result = response.getResult().stream().map(x -> {
//            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
//            BeanUtils.copyProperties(x,bean);
//            if(StringUtil.isNotEmpty(x.getScItemRootCategoryId())){
//                bean.setScItemCategoryId(x.getScItemRootCategoryId());
//                bean.setScItemCategoryName(x.getScItemRootCategoryName());
//            }
//            if(StringUtil.isNotEmpty(x.getScItemLeafCategoryId())){
//                bean.setScItemCategoryId(x.getScItemLeafCategoryId());
//                bean.setScItemCategoryName(x.getScItemLeafCategoryName());
//            }
//            return bean;
//        }).collect(Collectors.toList());
//        return PageResult.of(result,response.getTotal());
//    }

    private MonthSalesBasicReportLoadListVO loadListRequestConverter(QueryMonthSalesReportRequest request){
        MonthSalesBasicReportLoadListVO listVO = MonthSalesBasicReportLoadListVO.newMonthSalesBasicReportLoadListVOBuilder()
                .orgBusinessUnitIds(request.getOrgBusinessUnitIds())
                .orgSalesOrganizationIds(request.getOrgSalesOrganizationIds())
                .orgSalesChannelIds(request.getOrgSalesChannelIds())
                .orgSalesDepartmentIds(request.getOrgSalesDepartmentIds())
                .manageEmployeeIds(request.getManageEmployeeIds())
                .customerCodes(request.getCustomerCodes())
                .customerAddressCodes(request.getCustomerAddressCodes())
                .customerAddressCode(request.getCustomerAddressCode())
                .scItemCodes(request.getScItemCodes())
                .scItemOutCode(request.getScItemOutCode())
                .scItemRootCategoryIds(request.getScItemRootCategoryIds())
                .scItemLeafCategoryIds(request.getScItemLeafCategoryIds())
                .statisticalDate(request.getStatisticalDate())
                .dataMark(request.getDataMark())
                .groupBy(request.getGroupBy())
                .build();
        listVO.setStart(request.getStart());
        listVO.setLimit(request.getLimit());
        return listVO;
    }

    private MonthSalesBasicReportLoadListRequest getRequestConverter(QueryMonthSalesReportRequest request){
        MonthSalesBasicReportLoadListRequest listRequest = MonthSalesBasicReportLoadListRequest.newMonthSalesBasicReportLoadListRequestBuilder()
                .orgBusinessUnitId(request.getOrgBusinessUnitId())
                .orgSalesOrganizationId(request.getOrgSalesOrganizationId())
                .orgSalesChannelId(request.getOrgSalesChannelId())
                .orgSalesDepartmentId(request.getOrgSalesDepartmentId())
                .manageEmployeeId(request.getManageEmployeeId())
                .customerCode(request.getCustomerCode())
                .customerAddressCode(request.getCustomerAddressCode())
                .scItemCode(request.getScItemCode())
                //.scItemRootCategoryId((String)request.getScItemRootCategoryIds().get(0))
                //.scItemLeafCategoryId((String)request.getScItemLeafCategoryIds().get(0))
                .planTime(request.getPlanMonth())
                .statisticalDate(request.getStatisticalDate())
                .beginDate(request.getBeginDate())
                .endDate(request.getEndDate())
                .build();
        return listRequest;
    }


}
